using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using Peradigm.Framework.Domain.Parties;

using Liberty.Domain.ApplicationManagement;
using Liberty.Domain.ServiceMemberManagement;
using Liberty.Persistence.Kernel;

using Microsoft.VisualStudio.TestTools.UnitTesting;


namespace Tests.PersistenceTests.Kernel
{
    [TestClass()]
    public class PersonBrokerTests
    {
        #region Properties

        protected string ConnectionString
        {
            get
            {
                return ConfigurationSettings.AppSettings[CONNECTION_STRING];
            }
        }

        #endregion

        #region Setup and Teardown
        [TestInitialize()]
        public void SetupPersonBrokerTests()
        {
        }

        [TestCleanup()]
        public void TeardownPersonBrokerTests()
        {
        }
        #endregion

        #region Test Methods

        [TestMethod()]
        public void TestApplicantWith()
        {
            PersonBroker broker = new PersonBroker( this.ConnectionString );
            Applicant applicant = broker.ApplicantWith( TEST_APPLICANT_ID );
            Assert.AreEqual( TEST_APPLICANT_ID, applicant.Oid );
        }

        [TestMethod()]
        public void TestServiceMemberWith()
        {
            PersonBroker broker = new PersonBroker( this.ConnectionString );
            ServiceMember serviceMember = broker.ServiceMemberWith( TEST_SERVICE_MEMBER_ID );
            Assert.AreEqual( TEST_SERVICE_MEMBER_ID, serviceMember.Oid );
        }

        [TestMethod()]
        public void TestSaveServiceMember()
        {
            try
            {
                PersonBroker broker = new PersonBroker( this.NewTransaction );

                ServiceMember serviceMember = broker.ServiceMemberWith( TEST_SERVICE_MEMBER_ID );
                Assert.AreEqual( TEST_SERVICE_MEMBER_ID, serviceMember.Oid );
                broker.Save( serviceMember );

                serviceMember = broker.ServiceMemberWith( TEST_SERVICE_MEMBER_ID );
                Assert.AreEqual( TEST_SERVICE_MEMBER_ID, serviceMember.Oid );
            }
            finally
            {
                this.Rollback();
            }
        }

        [TestMethod()]
        public void TestSaveNewServiceMember()
        {
            try
            {
                PersonBroker broker = new PersonBroker( this.NewTransaction );
                ServiceBroker serviceBroker = new ServiceBroker( this.NewTransaction );

                ServiceMember serviceMember = new ServiceMember();
                serviceMember.FirstName = TEST_FIRST_NAME;
                serviceMember.MiddleName = TEST_MIDDLE_NAME;
                serviceMember.LastName = TEST_LAST_NAME;
                serviceMember.BornOn = Convert.ToDateTime( TEST_BORN_ON );
                serviceMember.Rank = serviceBroker.RankWith( TEST_RANK_ID );
                serviceMember.UnitAssignment = Unit.TestInstance();

                serviceMember.CurrentAddress = new Liberty.Domain.Kernel.Address(
                    TEST_PRIMARY_STREET_ADDRESS1,
                    TEST_PRIMARY_STREET_ADDRESS2,
                    TEST_PRIMARY_CITY,
                    TEST_PRIMARY_STATE,
                    TEST_PRIMARY_ZIP );

                serviceMember.HomeOfRecord = new Liberty.Domain.Kernel.Address(
                    TEST_PRIMARY_STREET_ADDRESS1,
                    TEST_PRIMARY_STREET_ADDRESS2,
                    TEST_PRIMARY_CITY,
                    TEST_PRIMARY_STATE,
                    TEST_PRIMARY_ZIP );

                serviceMember.PrimaryPhoneNumber = new UnvalidatedPhoneNumber( TEST_PRIMARY_PHONE );
                serviceMember.SecondaryPhoneNumber = new UnvalidatedPhoneNumber( TEST_SECONDARY_PHONE );

                broker.Save( serviceMember );

                ServiceMember testServiceMember = broker.ServiceMemberWith( serviceMember.Oid );
                Assert.AreEqual( serviceMember.Oid, testServiceMember.Oid );
#warning Add logic to test that all other properties were saved
            }
            finally
            {
                this.Rollback();
            }
        }
        [TestMethod()]
        public void TestServiceMembersMatching()
        {
            PersonBroker broker = new PersonBroker( this.ConnectionString );
            ServiceMemberSearchCriteria serviceMemberSearchCriteria =
                new ServiceMemberSearchCriteria( TEST_CRITERIA_LAST_NAME );
            List<ServiceMember> serviceMembers = broker.ServiceMembersMatching( serviceMemberSearchCriteria );
        }

        #endregion

        #region Support Methods
        private SqlConnection CurrentConnection
        {
            get
            {
                if( i_cxn == null )
                {
                    i_cxn = new SqlConnection( this.ConnectionString );
                    i_cxn.Open();
                }
                return i_cxn;
            }
        }

        private SqlTransaction NewTransaction
        {
            get
            {
                if( i_txn == null )
                {
                    i_txn = this.CurrentConnection.BeginTransaction();
                }
                return i_txn;
            }
        }

        private void Rollback()
        {
            if( i_txn != null )
            {
                i_txn.Rollback();
                i_txn.Dispose();
                i_txn = null;
            }
        }

        private void Commit()
        {
            if( i_txn != null )
            {
                i_txn.Commit();
                i_txn.Dispose();
                i_txn = null;
            }
        }

        private void CloseConnection()
        {
            if( i_cxn != null )
            {
                this.Rollback();
                i_cxn.Close();
                i_cxn.Dispose();
                i_cxn = null;
            }
        }
        #endregion

        #region Data Elements
        private SqlConnection i_cxn;
        private SqlTransaction i_txn;
        #endregion

        #region Constants

        private const string CONNECTION_STRING = "ConnectionString";

        private const int TEST_SERVICE_MEMBER_ID = 15;
        private const string TEST_NAME = "NUnit.Test";
        private const string TEST_ABBREVIATION = "NT";
        private DateTime? TEST_DATE;
        private const string TEST_CRITERIA_LAST_NAME = "Malloy";

        private const int TEST_BRANCH_ID = 1;
        private const int TEST_RANK_ID = 1;
        private const string TEST_FIRST_NAME = "First";
        private const string TEST_MIDDLE_NAME = "Middle";
        private const string TEST_LAST_NAME = "Last";
        private const string TEST_BORN_ON = "1/2/1980";
        private const string TEST_PRIMARY_PHONE = "972-555-1212";
        private const string TEST_PRIMARY_STREET_ADDRESS1 = "Primary address 1";
        private const string TEST_PRIMARY_STREET_ADDRESS2 = "Primary address 2";
        private const string TEST_PRIMARY_CITY = "Test city";
        private const string TEST_PRIMARY_STATE = "TX";
        private const string TEST_PRIMARY_ZIP = "75075";
        private const string TEST_PRIMARY_COUNTRY = "USA";
        private const string TEST_SECONDARY_PHONE = "972-577-0000";
        private const string TEST_SECONDARY_STREET_ADDRESS1 = "Secondary address 1";
        private const string TEST_SECONDARY_STREET_ADDRESS2 = "Secondary address 2";
        private const string TEST_SECONDARY_CITY = "Cincinnati";
        private const string TEST_SECONDARY_STATE = "OH";
        private const string TEST_SECONDARY_ZIP = "45212";
        private const string TEST_SECONDARY_COUNTRY = "USA";

        private const int TEST_APPLICANT_ID = 1;

        #endregion
    }
}
